<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>商品列表</title>

    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }


        h1 {
            width: 1200px;
            height: 90px;
            margin: 0 auto;
            background-color: skyblue;
            color: #fff;
            display: flex;
            justify-content: center;
            align-items: center;
            position: relative;
            font-size: 40px;
        }

        h1>p {
            font-size: 20px;
            position: absolute;
            top: 50%;
            transform: translateY(-50%);
            right: 50px;
            text-decoration: none;
            display: block;
        }




        .back {
            color: red;
            text-decoration: none;
            z-index: 1;
        }

        .goods-list {
            width: 1200px;
            margin: 20px auto;
            display: flex;
            flex-wrap: wrap;
            gap: 20px;
            justify-content: center;
            align-items: center;
            position: relative;
        }

        .goods-item {
            width: 280px;
            border: 1px solid #ddd;
            padding: 10px;
        }

        .goods-item img {
            width: 100%;
            height: 280px;
            object-fit: cover;
        }

        .goods-info {
            margin-top: 10px;
        }

        .goods-title {
            font-size: 14px;
            margin-bottom: 10px;
            height: 40px;
            overflow: hidden;
        }

        .price {
            color: red;
            font-size: 20px;
        }

        .original-price {
            color: #999;
            text-decoration: line-through;
            margin-left: 10px;
        }

        .pagination {
            border: 1px solid #333;
            margin: 10px auto;
            width: 1200px;
            height: 80px;
            background-color: #fff;
            color: #fff;
            display: flex;
            justify-content: flex-start;
            padding-left: 20px;
            box-sizing: border-box;
            align-items: center;
        }

        .pagination button {
            padding: 5px 15px;
            background: skyblue;
            border: none;
            cursor: pointer;
        }

        .pagination button:disabled {
            background: #ccc;
            cursor: not-allowed;
        }


        .page-info {
            padding: 5px 10px 5px 10px;
            font-size: 16px;
            margin-left: 12px;
            color: #333;

        }

        .page-size {
            padding: 5px 10px 5px 10px;
            font-size: 16px;
            margin-left: 12px;
        }

        .tags {
            position: absolute;
            top: 0;
            right: 0;
        }

        .tag {
            padding: 2px 5px;
            color: white;
            font-size: 12px;
        }

        .tag-hot {
            background-color: red;
        }

        .tag-discount {
            background-color: orange;
        }

        h2 {
            width: 1200px;
            margin: 0 auto;
            background-color: skyblue;
            color: #fff;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100px;
            font-size: 40px;
        }
    </style>

</head>

<body>
    <h1>
        <div class="header">仿站电商·商品列表</div>
        <p class="off active"><a href="views\index.html">回到首页</a></p>
    </h1>

    <div class="pagination">
        <button class="first-page">首页</button>
        <span>&nbsp;&nbsp;&nbsp;&nbsp;</span>
        <button class="prev-page">上一页</button>
        <span>&nbsp;&nbsp;&nbsp;&nbsp;</span>
        <span class="page-info">1 / 77</span>
        <span>&nbsp;&nbsp;&nbsp;&nbsp;</span>
        <button class="next-page">下一页</button>
        <span>&nbsp;&nbsp;&nbsp;&nbsp;</span>
        <button class="last-page">末页</button>
        <span>&nbsp;&nbsp;&nbsp;&nbsp;</span>
        <input type="text" class="jump-input" style="width: 50px; height: 25px;">
        <span>&nbsp;&nbsp;&nbsp;&nbsp;</span>
        <button class="jump-btn">跳转</button>
        <select class="page-size">
            <option value="4">4</option>
            <option value="8">8</option>
            <option value="12">12</option>
            <option value="16">16</option>
            <option value="20">20</option>
        </select>
    </div>

    <div class="goods-list" id="goodsList">
        <!-- 商品列表将通过 JavaScript 动态生成 -->
    </div>

    <div class="goods-list" id="goodsList">
        <!-- 商品列表将通过 JavaScript 动态生成 -->
    </div>

    <h2>
        <div class="fooder">网页底部</div>
    </h2>

    <script src="js\goods.js"></script>
    <script>
        // 获取商品数据 - 使用全局的list变量（来自goods.js）
        let goodsList = list || [];  // 使用goods.js中定义的list变量
        let currentPage = 1;
        let pageSize = 12;
        let totalPages = Math.ceil(goodsList.length / pageSize);

        // 渲染商品列表
        function renderGoods() {
            const container = document.getElementById('goodsList');
            const start = (currentPage - 1) * pageSize;
            const end = start + pageSize;
            const pageGoods = goodsList.slice(start, end);

            //使用map方法遍历pageGoods数组
            container.innerHTML = pageGoods.map(item => `
              <div class="goods-item" onclick="location.href='views/detail.html?id=${item.goods_id}'">
                    <div style="position: relative;">
                        <img src="${item.img_big_logo}" alt="${item.title}">
                      <div class="tags">
                          ${item.is_hot ? '<span class="tag tag-hot">热销</span>' : ''}
                          ${item.is_sale ? '<span class="tag tag-discount">折扣</span>' : ''}
                      </div>
                  </div>
                  <div class="goods-info">
                      <div class="goods-title">${item.title}</div>
                      <div>
                          <span class="price">￥${item.current_price}</span>
                          <span class="original-price">￥${item.price}</span>
                      </div>
                  </div>
              </div>
          `).join('');
        }

        // 更新分页信息
        function updatePagination() {
            document.querySelector('.page-info').textContent = `${currentPage} / ${totalPages}`;
            document.querySelector('.prev-page').disabled = currentPage === 1;
            document.querySelector('.first-page').disabled = currentPage === 1;
            document.querySelector('.next-page').disabled = currentPage === totalPages;
            document.querySelector('.last-page').disabled = currentPage === totalPages;
        }

        // 事件监听
        document.querySelector('.first-page').addEventListener('click', () => {
            currentPage = 1;
            renderGoods();
            updatePagination();
        });

        document.querySelector('.prev-page').addEventListener('click', () => {
            if (currentPage > 1) {
                currentPage--;
                renderGoods();
                updatePagination();
            }
        });

        document.querySelector('.next-page').addEventListener('click', () => {
            if (currentPage < totalPages) {
                currentPage++;
                renderGoods();
                updatePagination();
            }
        });

        document.querySelector('.last-page').addEventListener('click', () => {
            currentPage = totalPages;
            renderGoods();
            updatePagination();
        });

        document.querySelector('.jump-btn').addEventListener('click', () => {
            const page = parseInt(document.querySelector('.jump-input').value);
            if (page && page > 0 && page <= totalPages) {
                currentPage = page;
                renderGoods();
                updatePagination();
            }
        });

        document.querySelector('.page-size').addEventListener('change', (e) => {
            pageSize = parseInt(e.target.value);
            totalPages = Math.ceil(goodsList.length / pageSize);
            currentPage = 1;
            renderGoods();
            updatePagination();
        });

        // 初始化
        renderGoods();
        updatePagination();
    </script>

</body>

</html>